package com.ruoyi.common.websocket.config;

import com.ruoyi.common.core.core.domain.model.LoginUser;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.security.utils.LoginHelper;
import com.ruoyi.common.core.core.domain.model.UserPrincipal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;

import java.security.Principal;
import java.util.Map;

import static com.ruoyi.common.websocket.constant.WebSocketConstants.LOGIN_USER_KEY;

@Slf4j
public class MyChannelInterceptorChannelInterceptor implements ChannelInterceptor {

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        //自定义 ChannelInterceptor 可以使用 StompHeaderAccessor 或 SimpMessageHeaderAccessor 访问有关消息的信息
        StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
//        StompHeaderAccessor accessor2 = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        Map<String, Object> sessionAttributes = accessor.getSessionAttributes();
        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            String token = (String) accessor.getFirstNativeHeader("Authorization");
            if(token==null){
                log.info("token is null");
                throw new ServiceException("token is null");
            }
            LoginUser loginUser = LoginHelper.getLoginUser(token);
            if(loginUser!=null){
                log.info("CONNECT_SUCCESS");
                UserPrincipal principal = MapstructUtils.convert(loginUser, UserPrincipal.class);
                accessor.setUser(principal);
                sessionAttributes.put(LOGIN_USER_KEY, principal);
            }else{
                log.info("CONNECT_FAIL");
                throw new ServiceException("token is error");
            }
        }
        if(sessionAttributes.containsKey(LOGIN_USER_KEY)){
            accessor.setUser((Principal) sessionAttributes.get(LOGIN_USER_KEY));
        }
        if(StompCommand.SEND.equals(accessor.getCommand())){
            String destination = accessor.getDestination();
            if(destination.startsWith("/appWs")){
                return message;
            }else{
                log.info("禁止客户端发送广播消息"+destination);
                return null;
            }
        }
        //订阅
        if(StompCommand.DISCONNECT.equals(accessor.getCommand())){
            //订阅消息要判断订阅地址是否和用户登录token信息一致
            
        }
        return message;
    }

    @Override
    public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
        ChannelInterceptor.super.postSend(message, channel, sent);
    }

    @Override
    public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
        ChannelInterceptor.super.afterSendCompletion(message, channel, sent, ex);
    }

    @Override
    public boolean preReceive(MessageChannel channel) {
        return ChannelInterceptor.super.preReceive(channel);
    }

    @Override
    public Message<?> postReceive(Message<?> message, MessageChannel channel) {
        return ChannelInterceptor.super.postReceive(message, channel);
    }

    @Override
    public void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex) {
        ChannelInterceptor.super.afterReceiveCompletion(message, channel, ex);
    }
}
